<template>
  <div class="orders-container">
    <h2>我的订单</h2>
    <div v-if="orders.length > 0">
      <div class="order-list">
        <div v-for="order in orders" :key="order.id" class="order-item">
          <div class="order-header">
            <span class="order-id">订单号: #{{ order.id }}</span>
            <span class="order-date">{{ formatDate(order.createdAt) }}</span>
            <span class="order-status" :class="order.status">{{ order.status }}</span>
          </div>
          <div class="order-items">
            <div v-for="item in order.items" :key="item.id" class="order-product">
              <img :src="item.image" :alt="item.name" class="product-image">
              <div class="product-info">
                <h4>{{ item.name }}</h4>
                <p>¥{{ item.price }} × {{ item.quantity }}</p>
              </div>
              <div class="product-total">
                ¥{{ (item.price * item.quantity).toFixed(2) }}
              </div>
            </div>
          </div>
          <div class="order-footer">
            <div class="order-total">
              总计: <span>¥{{ order.total.toFixed(2) }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="empty-orders">
      <p>您还没有订单</p>
      <router-link to="/products">去购物</router-link>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { fetchUserOrders } from '../api/mockData'

export default {
  setup() {
    const orders = ref([])

    onMounted(async () => {
      const userId = localStorage.getItem('userId')
      if (userId) {
        orders.value = await fetchUserOrders(userId)
      }
    })

    const formatDate = (dateString) => {
      const options = { year: 'numeric', month: 'long', day: 'numeric' }
      return new Date(dateString).toLocaleDateString(undefined, options)
    }

    return { orders, formatDate }
  }
}
</script>

<style scoped>
.orders-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}
.order-list {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}
.order-item {
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
}
.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background: #f9f9f9;
  border-bottom: 1px solid #eee;
}
.order-id {
  font-weight: bold;
}
.order-date {
  color: #666;
  font-size: 0.9rem;
}
.order-status {
  padding: 0.3rem 0.8rem;
  border-radius: 4px;
  font-size: 0.9rem;
  text-transform: capitalize;
}
.order-status.completed {
  background: #e8f5e9;
  color: #2e7d32;
}
.order-status.processing {
  background: #fff8e1;
  color: #ff8f00;
}
.order-status.cancelled {
  background: #ffebee;
  color: #c62828;
}
.order-items {
  padding: 1rem;
}
.order-product {
  display: flex;
  align-items: center;
  padding: 0.5rem 0;
  gap: 1rem;
}
.product-image {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
}
.product-info {
  flex: 1;
}
.product-info h4 {
  margin: 0;
  font-size: 1rem;
}
.product-info p {
  margin: 0.3rem 0 0;
  color: #666;
  font-size: 0.9rem;
}
.product-total {
  font-weight: bold;
  min-width: 80px;
  text-align: right;
}
.order-footer {
  display: flex;
  justify-content: flex-end;
  padding: 1rem;
  border-top: 1px solid #eee;
}
.order-total {
  font-size: 1.1rem;
}
.order-total span {
  font-weight: bold;
  color: #e53935;
}
.empty-orders {
  text-align: center;
  padding: 4rem 0;
}
.empty-orders p {
  font-size: 1.2rem;
  color: #666;
  margin-bottom: 1rem;
}
.empty-orders a {
  color: #42b983;
  text-decoration: none;
}
.empty-orders a:hover {
  text-decoration: underline;
}
</style>
